using System;
using System.Collections.Generic;
using GearOptimizer.ExtensionMethods;

namespace GearOptimizer.Adapters
{
    public class MemoryRepository<T> : IRepository<T> where T : IIdentifiable
    {
        private readonly Dictionary<string, T> dictionary = new Dictionary<string,T>();

        public T Get(string id)
        {
            T returnValue;

            var didGetValue = dictionary.TryGetValue(id, out returnValue);

            if (!didGetValue)
            {
                returnValue = default(T);
            }

            return returnValue;
        }

        public void Set(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value cannot be null");
            }
            dictionary.Add(value.Id, value.DeepCopy());
        }

        public void Remove(string id)
        {
            dictionary.Remove(id);
        }

        public bool Contains(string id)
        {
            return dictionary.ContainsKey(id);
        }
    }
}